<?php

/**
 * Created: 16-mei-2007
 * Author: Jeroen van der Laan
 *
 **/

require_once('php/lib/mvc/model.php');
config('Helios', 'helios');

class Helios extends Model {

	/**
	 * Opens the database connection
	 */
	private function setDatabase($db)
	{
		$this->database = $db;
	}

	function getFile($value) {		
		$query = "SELECT * FROM `file` WHERE `name` LIKE '%$value%'";
		//printf('query: '.$query);
		return $this->all($query);
	}
	
	function getFolder($value) {
		$query = 'SELECT * FROM folder WHERE name LIKE \'%'.$value.'%\'';
		//printf('query: '.$query);
		return $this->all($query);
	}
	
	function getTrack($value) {
		$query = 'SELECT * FROM track WHERE title LIKE \'%'.$value.'%\'';
		//printf('query: '.$query);
		return $this->all($query);
	}
	
	// FIXME: doesn't work!
	function getArtist($value) {
		$query = 'SELECT * FROM artist WHERE name LIKE \'%'.$value.'%\'';
		//printf('query: '.$query);
		return $this->all($query);
	}
	
	function getAlbum($value) {
		$query = 'SELECT * FROM album WHERE title LIKE \'%'.$value.'%\'';
		//printf('query: '.$query);
		return $this->all($query);
	}
	
	function getFolderFromId($parentid)
	{
		$return = "/";
		
		if ($parentid == 0)
		{
			$return += $this->getFolderById($parentid);
			return $return;
		} else {
			$i = $parentid;
			while ($i != 0)
			{
				$folder = $this->getFolderById($i);
				$return = "/" . $folder['name'] . $return; 		
				$i = $folder['parent'];	
			}
			return $return;
		}
	}
	
	private function getFolderById($id)
	{
		$this->query_fields = '*';
		$this->table_name = 'folder';
		$this->id_field = 'id';
		$this->do_not_prefix = array('id', 'name', 'parent');
		return $this->getById($id);
	}
	
	function getFoldersByParentId($parentid)
	{
		$this->query_fields = '*';
		$this->table_name = 'folder';
		$this->id_field = 'parent';
		$this->do_not_prefix = array('id', 'name', 'parent');
		return $this->allOrderedBy('name', 'parent='.$parentid);
	}
	
	function getArtistById($id)
	{
		$this->query_fields = '*';
		$this->table_name = 'artist';
		$this->id_field = 'id';
		$this->do_not_prefix = array('id', 'name');
		return $this->getById($id);
	}
	
	// TODO: Album & Track koppelen
	function getAlbumById($id)
	{
		$this->query_fields = '*';
		$this->table_name = 'album';
		$this->id_field = 'id';
		$this->do_not_prefix = array('id', 'name');
		return $this->getById($id);
	}
	
	function getAlbumsFromArtist($artistid)
	{
		$query = 'SELECT * FROM album WHERE artist = '.$artistid;
		return $this->all($query);
	}
}

?>